/**
 * Created by zyx on 2017/9/18.
 */
angular.module('module.auth')
    .controller('LoginController', LoginController);

LoginController.$inject = ['$state', 'AuthService', '$localStorage'];

// 登录
function LoginController($state, AuthService, $localStorage) {

    var vm = this;
    vm.sureBtn = sureBtn; // 模态框
    vm.cancelBtn = cancelBtn; // 模态框
    vm.login = login;
    vm.seeEye = seeEye; // 密码可见

    function sureBtn() {
        vm.messageVisible = false;
    }

    function cancelBtn() {
        vm.messageVisible = false;
    }

    vm.user = {
        username: "",
        password: "",
        acctype: 6
    };
    vm.cansee = false;
    document.getElementById("password").type = "password";

    function seeEye() {
        vm.cansee = !vm.cansee;
        if (vm.cansee == true) {
            document.getElementById("password").type = "text";
        } else {
            document.getElementById("password").type = "password";
        }
    }

    function login(user) {
        if (!vm.user.username) {
            modal(true, "账号不能为空！");
            return;
        }
        if (!vm.user.password) {
            modal(true, "密码不能为空！");
            return;
        }
        AuthService.login(vm.user, function (success) {
            $localStorage[environmental + "userid"] = parseInt(success.userid);
            AuthService.getMerchantId(success.userid, function (sucData) {
                if (!sucData.domainQueryInfoByMerchids[0].shopid) {
                    alert("还未开通！")
                } else {
                    $localStorage[environmental + "shopid"] = sucData.domainQueryInfoByMerchids[0].shopid;
                    $state.go('homepage');
                }
            }, function (error) {
                modal(true, error.data.msg);
            })
        }, function (error) {
            modal(true, error.data.msg);
            vm.user = {
                username: "",
                password: "",
                acctype: 6
            };
        })
    }

    // 提示模态框控制
    function modal(status, msg) {
        vm.messageVisible = status;
        vm.message = msg;
    }
}